package com.lier.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lier.gulimall.product.entity.AttrEntity;
import com.lier.gulimall.product.service.AttrService;
import com.lier.gulimall.product.service.CategoryService;
import com.lier.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.lier.gulimall.product.vo.AttrRelationVo;
import com.lier.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lier.gulimall.product.entity.AttrGroupEntity;
import com.lier.gulimall.product.service.AttrGroupService;
import com.lier.common.utils.PageUtils;
import com.lier.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author lier
 * @email 664478853@qq.com
 * @date 2025-06-06 14:14:16
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;

    /**
     * 列表
     */
    @GetMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> entities = attrGroupService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    @GetMapping("{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params, @PathVariable Long attrgroupId){
        PageUtils page = attrService.getRelationNoAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    @GetMapping("{catelogId}/withattr")
    public R withAttr(@PathVariable Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getGroupWithAttrs(catelogId);
        return R.ok().put("data", vos);
    }


    @PostMapping("attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrRelationVo> attrRelationVoList){
        attrGroupService.removeAttrRelation(attrRelationVoList);
        return R.ok();
    }

    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrRelationVo> attrRelationVoList){
        attrGroupService.saveRelation(attrRelationVoList);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> catelogPath =  categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.save(attrGroupEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
